package insight.chain.validation;

import insight.chain.exception.ValidatorException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author huu
 * @create 2025-08-25-19:12
 * @Description： 这个上下文在责任链中可以传递，每一个节点可以影响流程，流程汇总
 * TODO 实现一个总上下文来管理不同字段的校验链，感觉可以在这里面加一个Map，键为字段名，值为校验链？
 */
public class ValidatorContext {

    private List<String> errorList = new ArrayList<>();

    private boolean stop = false;

    private int index;

    private Object value;

    private HashMap<String, Object> data = new HashMap<>();
    
    public ValidatorContext(Object value) {
        this.value = value;
    }
    public void appendError(String s) {
        errorList.add(s);
    }

    public void throwsExceptionIfNecessary() throws ValidatorException{
        if(errorList.isEmpty()){
            return;
        }
        throw new ValidatorException(errorList.toString());

    }

    //当上一个节点主动调用donext那么责任链就会继续往下走，和Stop是一样的
    public void donext(Object value) {
        index++;
        this.value = value;
    }
    public int currentIndex(){
        return index;
    }
    public void stopChain() {
        this.stop = true;
    }
    public boolean shouldStop(){
        return stop;
    }

    public Object getValue() {
        return value;
    }

    public void put(String key, Object value){
    	data.put(key, value);
    }
    public Object get(String key){
    	return data.get(key);
    }
}
